package controllers

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	"goravel/app/models"
	"goravel/app/services"
	"goravel/app/types"
)

type SysOperateLogController struct {
	DbServer services.DBServer
}

func NewSysOperateLogController() *SysOperateLogController {
	return &SysOperateLogController{
		DbServer: services.NewDBService(),
	}
}

// Index 操作日志列表
// @Summary      操作日志列表
// @Tags         SysOperateLog
// @Router       /sys_operate_log [get]
func (c *SysOperateLogController) Index(ctx http.Context) http.Response {
	var modelList []models.SysOperateLog
	query := facades.Orm().Query()
	//其他查询条件
	query = query.OrderBy("id", "desc")
	data, meta, err := c.DbServer.ListPage(ctx, &modelList, query, nil)
	if err != nil {
		return services.NewApiService(ctx).Error500(err.Error())
	}
	return services.NewApiService(ctx).DataPage(data, meta)
}

// Show 操作日志详情
// @Summary      操作日志详情
// @Tags         SysOperateLog
// @Router       /sys_operate_log [get]
func (c *SysOperateLogController) Show(ctx http.Context) http.Response {
	var model models.SysOperateLog
	id := ctx.Request().RouteInt("id")
	err := facades.Orm().Query().FindOrFail(&model, id)
	if err != nil {
		return services.NewApiService(ctx).Error500(err.Error())
	}
	return services.NewApiService(ctx).Data(model)
}

// Store 新增操作日志
// @Summary      新增操作日志
// @Tags         SysOperateLog
// @Router       /sys_operate_log [post]
func (c *SysOperateLogController) Store(ctx http.Context) http.Response {
	var createReq types.SysOperateLogCreateReq
	var err error
	var model models.SysOperateLog
	err = ctx.Request().Bind(&createReq)
	if err != nil {
		return services.NewApiService(ctx).Error500("参数错误")
	}
	err = c.DbServer.Create(&model, &createReq)
	if err != nil {
		return services.NewApiService(ctx).Error500(err.Error())
	}
	return services.NewApiService(ctx).OK("stored")
}
